<?php

namespace interface\v1;

/*
*节假日接口类
*/

class holidayAppClass extends index
{
    private const table = 'holiday';
    private const secretKey = 'yumukeji+2014';//秘钥
    private const secretId = 'yumuApiId';//ID

    /*生成签名*/
    private function signature($params): string
    {
        return self::getApiSign($params);
    }

    public static function getApiSign(array $params): string
    {
        ksort($params); //按照key升序排列
        $strTemp = '';
        foreach ($params as $key => $item) {
            $strTemp .= $key . '=' . $item . '&'; //整合新的参数数组
        }
        $signTemp = $strTemp . "secretKey=" . self::secretKey; //拼接secret
        return md5($signTemp);
    }

    /**
     * 返回
     */
    public function holidayDate()
    {
        $sign = $this->get('sign') ?: 1;//1:提供节假日，2：提供含周末的节假日
        $year = $this->get('year');
        $timestamp = $this->get('timestamp');
        $params = [
            'timestamp' => $timestamp,
            'sign' => $sign,
            'year' => $year,
            'secretId' => self::secretId,
        ];
        if (empty($this->get('sign'))) {
            unset($params['sign']);
        }
        $signature = $this->signature($params);
        if ($signature !== $this->get('signature')) {
            $this->resp['warn'] = '签名错误';
        } elseif ($timestamp + 60 < time()) {
            $this->resp['warn'] = '签名已过期';
        } elseif (empty($year) || is_integer($year)) {
            $this->resp['warn'] = '请输入正确的年份';
        } else {
            $info = query(self::table, "year = '$year'");
            if (empty($info['id'])) {
                $this->resp['warn'] = '还未设置当前年份的节假日';
            } else {
                $festivals = $info['festival'] ? json_decode($info['festival'], true) : [];
                if ($sign == 1) {
                    $workDay = $info['workDay'] ? json_decode($info['workDay'], true) : [];
                    $data['dateWithWeekend'] = $workDay;
                } else {
                    $data['dateWithWeekend'] = [];
                }
                $data['date'] = array_keys($festivals);
                $data['holidayText'] = $festivals;
                $data['year'] = $info['year'];
                $this->code = 1;
                $this->resp['data'] = $data;
            }
        }
    }

    public function importTemplate()
    {
        $version = $this->get('version');//版本
        $timestamp = $this->get('timestamp');
        $params = [
            'timestamp' => $timestamp,
            'version' => $version,
            'secretId' => self::secretId,
        ];
        $signature = $this->signature($params);
        if ($signature !== $this->get('signature')) {
            $this->resp['warn'] = '签名错误';
        } elseif ($timestamp + 60 < time()) {
            $this->resp['warn'] = '签名已过期';
        } elseif (empty($version)) {
            $this->resp['warn'] = '未找到版本';
        } else {
            $list = [];
            $all = find(array('table' => 'import_template', 'where' => "version = '$version' order by list asc"));
            foreach ($all['array'] as $array){
                $list[] = [
                    'version'=>$array['version'],
                    'typeOne'=>$array['typeOne'],
                    'type'=>$array['type'],
                    'name'=>$array['name'],
                    'fileName'=>$array['fileName'],
                    'src'=>$array['src'],
                    'fullSrc'=>root.$array['src'],
                    'format'=>$array['format'],
                    'size'=>$array['size'],
                ];
            }
            $data['list'] = $list;
            $this->code = 1;
            $this->resp['data'] = $data;
        }
    }

}